package com.wyp.User;

import com.wyp.pojo.Admins;
import com.wyp.service.impl.AdminsServiceImpl;
import com.wyp.service.impl.ExamineesServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserDetailsService {


    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    @Autowired
    private AdminsServiceImpl adminsService;

    @Autowired
    private ExamineesServiceImpl examineesService;

    private  HttpServletRequest request;

    private AntPathMatcher antPathMatcher = new AntPathMatcher();

    public static HttpServletRequest getRequest() {
        return getRequestAttributes().getRequest();
    }

    public static ServletRequestAttributes getRequestAttributes() {
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        return (ServletRequestAttributes) attributes;
    }

    public UserServiceImpl() {
    }

    @Override
    public UserDetails loadUserByUsername(String usename) throws UsernameNotFoundException {
        request = getRequest();
        if (antPathMatcher.match("/student/student_index.html",request.getRequestURI())) {
            Map<String, Object> examinees = examineesService.findByStuaccount(usename);
            if (examinees == null) {
                throw new UsernameNotFoundException("考生号: '" + usename + "' 不存在!");
            }
            List<SimpleGrantedAuthority> authorities = new ArrayList<>();
            authorities.add(new SimpleGrantedAuthority("ROLE_STU"));
            return new User((int)examinees.get("stuid"),(String) examinees.get("stuaccount"), (String) examinees.get("stupassword"),true,true,true,true, authorities) ;
        }
        if (antPathMatcher.match("/admin/admin_index.html",request.getRequestURI())) {
            Admins admins = new Admins();
            admins.setAdminaccount(usename);
            admins = adminsService.findByAdminaccount(usename);
            List<SimpleGrantedAuthority> authorities = new ArrayList<>();
            if (adminsService.findByAdminaccount(usename) == null) {
                throw new UsernameNotFoundException("管理员账号: '" + usename + "' 不存在!");
            }
            authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
            if (admins.isIssuperadmin()) {
                authorities.add(new SimpleGrantedAuthority("ROLE_SUPERADMIN"));
            }
            if (admins.isChangeadminauth()) {
                authorities.add(new SimpleGrantedAuthority("ROLE_Cadmin"));
            }
            if (admins.isChangestuauth()) {
                authorities.add(new SimpleGrantedAuthority("ROLE_Cstu"));
            }
            if (admins.isChangetestauth()) {
                authorities.add(new SimpleGrantedAuthority("ROLE_Ctest"));
            }
            return new User(admins.getAdminid(),admins.getAdminaccount(), admins.getAdminpassword(), authorities,true,true,true,true) ;
        }
        return null;
    }
}
